import fs from 'fs'
import path from 'path'
import logger from 'morgan'
import dayjs from 'dayjs'

export default (app) => {
  const ENV = app.get('env')
  const logFileName = `access.${dayjs().format('YYYY-MM-DD')}.log`
  const logFilePath = path.join(__dirname, '../../logs', logFileName)
  if (ENV !== 'production') {
    // 开发环境用默认得输出流就行了
    // app.use(logger('combined'))
    app.use(logger('dev'))
  } else {
    // 创建写入流
    const writeStream = fs.createWriteStream(logFilePath, {
      flags: 'a' // 追加
    })
    // 把输出流改成我们得写入流；就可以写入文件了
    app.use(logger('combined', {
      stream: writeStream
    }))
  }
}
